package io.devops


def MavenBuild(){
    container('maven'){
        script {
            sh 'mvn  package -DskipTests -Dmaven.javadoc.skip=true -Dmaven.site.skip=true -Dmaven.source.skip=true -Djacoco.skip=true -Dcheckstyle.skip=true -Dfindbugs.skip=true -Dpmd.skip=true -Dfabric8.skip=true -e -B'
        }

    }

}

def buildImage(appName){
    // 构建镜像
    container('maven'){
        writeFile file:'Dockerfile', text:libraryResource("dockerfile/java")
        sh 'docker build -t docker.io/feeeng/${appName}:demo-$BUILD_NUMBER . '
        withCredentials(
            [usernamePassword(passwordVariable : 'DOCKER_PASSWORD' ,
                            usernameVariable : 'DOCKER_USERNAME' ,
                            credentialsId : "29915a10-e869-4b86-81f5-e0e3573a6c27" ,)]) {
                sh 'echo "$DOCKER_PASSWORD" | docker login $REGISTRY -u "$DOCKER_USERNAME" --password-stdin'
                sh 'docker push  ${registry}/${namespace}/${appName}:demo-$BUILD_NUMBER'
            }
    }
    // 镜像同步 todo
    // https://github.com/AliyunContainerService/image-syncer     

}


def main(buildType, appName){
    if(buildType == 'java'){
        MavenBuild()
    }
    buildImage(appName)
}